import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

public class RedisHash {
    /**
     * hset和hget
     * @param jedis
     */
    private static void testHsetAndHget(Jedis jedis){
        jedis.hset("key","name","zhangsan");
        jedis.hset("key","age","20");
        String name = jedis.hget("key", "name");
        System.out.println(name);
        String age = jedis.hget("key", "age");
        System.out.println(age);
    }

    /**
     * hexists 和 hdel
     * @param jedis
     */
    private static void testHexistAndHdel(Jedis jedis){
        jedis.hset("key","name","02");
        boolean hexists = jedis.hexists("key", "name");
        System.out.println(hexists);
        jedis.hdel("key","name");
        hexists= jedis.hexists("key","name");
        System.out.println(hexists);

    }

    /**
     * hkeys hvals
     * @param jedis
     */
    private static void testHkeysAndHvals(Jedis jedis){
        jedis.hset("key","name","zhangsan");
        jedis.hset("key","age","20");
        Set<String> key = jedis.hkeys("key");
        System.out.println(key);
        /**
         * 得到具体的值
         */
        List<String> key1 = jedis.hvals("key");
        System.out.println(key1);
    }
    private static void testHmget(Jedis jedis){
        jedis.hset("key","name","zhangsan");
        jedis.hset("key","age","20");
        //直接得到name和age的值
        List<String> hmget = jedis.hmget("key", "name", "age");
        System.out.println(hmget);

    }

    /**
     * hlen
     * @param jedis
     */
    private static void testHlen(Jedis jedis){
        jedis.hset("key","name","zhangsan");
        jedis.hset("key","age","20");
        long key = jedis.hlen("key");
        System.out.println(key);
    }
    private static void testHInceByAndIncrByFloat(Jedis jedis){
        jedis.hset("key","age","20");
        long l = jedis.hincrBy("key", "age", 10);
        System.out.println(l);
        String value = jedis.hget("key", "age");
        System.out.println(value);
//        jedis.hset("key","age","20");
        double m = jedis.hincrByFloat("key", "age", 0.5);
        System.out.println(m);
         value = jedis.hget("key", "age");
        System.out.println(value);
    }

    public static void main(String[] args) {
        JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis=jedisPool.getResource()){
//            testHsetAndHget(jedis);
//            testHexistAndHdel(jedis);
            testHkeysAndHvals(jedis);
//            testHmget(jedis);
//            testHmget(jedis);
//            testHlen(jedis);
//            testHInceByAndIncrByFloat(jedis);



        }
    }
}
